package com.holly.unit.gen.api.util;

import com.holly.unit.gen.api.constants.FreemarkerConstants;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

/**
 * 类描述: Freemarker格式化工具
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/1/14 10:58
 */
public class FreemarkerFormatUtil {


    /***
     * 下划线转为驼峰命名
     * @param param 下划线命名的参数
     * @return
     */
    public static String underline2Hump(String param) {
        StringBuilder sb = new StringBuilder();
        String arr[] = param.split(FreemarkerConstants.UNDERLINE);
        for (int i = 0; i < arr.length; i++) {
            String s = arr[i];
            if (param.contains(FreemarkerConstants.UNDERLINE)) {
                sb.append(s);
                continue;
            }
            if (sb.length() == 0) {
                sb.append(s.toLowerCase());
            } else {
                sb.append(s.substring(0,1).toUpperCase());
                sb.append(s.substring(1).toLowerCase());
            }
        }

        return sb.toString();
    }

    /***
     * 驼峰命名转为下划线命名
     * @param humpParam 驼峰命名
     * @return
     */
    public static String hump2Underline(String humpParam) {
        StringBuilder sb = new StringBuilder();
        int index=0;

        if (!humpParam.contains(FreemarkerConstants.UNDERLINE)) {
            for (int i = 0; i < humpParam.length(); i++) {
                if (Character.isUpperCase(humpParam.charAt(i))) {
                    sb.insert(i+index,FreemarkerConstants.UNDERLINE);
                    index+=1;
                }
            }
        }
        if (sb.toString().toLowerCase().startsWith(FreemarkerConstants.UNDERLINE)) {
            return sb.toString().toLowerCase().substring(1);
        }

        return sb.toString().toLowerCase();
    }


    /***
     * 驼峰命名转为横线命名
     * @param humpParam 参数
     * @return
     */
    public static String hump2Midline(String humpParam) {
        StringBuilder sb = new StringBuilder();
        int index=0;

        if (!humpParam.contains(FreemarkerConstants.MIDDLELINE)) {
            for (int i = 0; i < humpParam.length(); i++) {
                if (Character.isUpperCase(humpParam.charAt(i))) {
                    sb.insert(i+index,FreemarkerConstants.MIDDLELINE);
                    index+=1;
                }
            }
        }
        if (sb.toString().toLowerCase().startsWith(FreemarkerConstants.MIDDLELINE)) {
            return sb.toString().toLowerCase().substring(1);
        }

        return sb.toString().toLowerCase();
    }

    /***
     * 格式化数字 0.00
     * @param obj
     * @return
     */
    public static String numberFormat(Object obj) {
        obj = obj == null || obj.toString().length() == 0?0:obj;
        if (obj.toString().equalsIgnoreCase("NaN")) return "NaN";
        return new DecimalFormat("0.00").format(Double.parseDouble(obj.toString()));
    }

    /***
     * 格式化数字 0.00
     * @param obj
     * @return
     */
    public static String numberFormat(Object obj,String pattern) {
        obj = obj == null || obj.toString().length() == 0?0:obj;
        if (obj.toString().equalsIgnoreCase("NaN")) return "NaN";
        return new DecimalFormat(pattern).format(Double.parseDouble(obj.toString()));
    }

    /***
     * 格式化为金额
     * @param obj
     * @return
     */
    public static String moneyFormat(Object obj) {
        obj = obj == null || obj.toString().length() == 0?0:obj;
        return NumberFormat.getCurrencyInstance(Locale.CHINA).format(obj);
    }

    /***
     * 格式化百分比
     * @param obj
     * @return
     */
    public static String percentFormat(Object obj) {
        obj = obj == null || obj.toString().length() == 0?0:obj;
        if (obj.toString().equalsIgnoreCase("NaN")) return "";
        return NumberFormat.getPercentInstance(Locale.CHINA).format(obj);
    }


    /***
     * 把list中字符串转化为逗号分割字符串
     * @param param 参数
     * @return
     */
    public static String list2Str(List<String> param) {
        StringBuilder sb = new StringBuilder();
        for (String str:param) {
            sb.append("'"+str+"'");
        }
        String res = sb.toString();
        if (!Objects.isNull(res) || res.endsWith(FreemarkerConstants.DOT)) {
            res = res.substring(0,res.length()-1);
            return res;
        } else {
            return null;
        }
    }























}
